from utils.netFunction import *
from utils.readUtil import *


class NetFunction():
    def initNetFunction(self):
        self.userid = None
        self.header = None
        self.disHeader = None
        self.netInformation = None
        self.cookies = None
        self.user = {}
        self.getHeaderInfo()

    def getHeaderInfo(self):
        self.header = readHear("header/android/header")
        self.disHeader = readHear("header/disConnect/header")
        self.disHeader['User-Agent'] = self.header['User-Agent']

    def uiGetNetInfo(self):
        try:
            self.netInformation, self.cookies = getNetInformation()
            for i in self.netInformation:
                self.user[i.split("=")[0]] = i.split("=")[1]
        except ZeroDivisionError as e:
            self.netInformation = None
            self.terminal.append(str(e))
            self.terminal.append(str("请退出认证，手动获取"))


    def uiLoginNet(self,userid,passwd):
        if len(userid) == 0 or len(passwd) == 0:
            raise ZeroDivisionError("账号或密码不能为零")
        res = loginNet(userid, passwd, self.header)
        self.terminal.append(str(res['message']))
        if res['message'] is None:
            if userid not in self.accounts:
                writePsdFile('log/account.txt',userid+"-"+passwd)
        return res

    def uiDisNet(self, userid):
        if len(userid) == 0:
            raise ZeroDivisionError("账号不能为空")
        res = disConnect(acip='10.95.254.1',
                         userip=self.user['wlanuserip'],
                         acname=self.user['wlanacname'],
                         mac=self.user['mac'],
                         userid=self.userid,
                         header=self.disHeader)
        self.terminal.append(str(res['message']))
        return res
